package com.example.gfbov;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class GameView extends View {
    //Canvas 画布
    //Graphics 图形
    //Paint 画笔
    //Bitmap 位图
    //Thread 线程

    private Paint paint;
    private float screenwidth, screenheight;//屏幕尺寸
    private float x, y;//矩形的顶点
    private float lineX, lineY;//直线端点
    private float radius;//圆的半径
    private boolean flag = true;//线程控制标志
    private long speed = 1;//每秒帧数
    private int Cdir = 1;
    private int walk=1;


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制矩形
        paint.setColor(Color.BLUE);
        canvas.drawRect(x, y, x + 100, y + 100, paint);
        //绘制圆形
        paint.setColor(Color.RED);
        canvas.drawCircle(screenwidth / 2, screenheight / 2, radius, paint);
        //绘制直线
        paint.setColor(Color.BLACK);
        canvas.drawLine(lineX, lineY, screenwidth, screenheight, paint);
    }

    //getx/y 相对坐标  getRawX/RawY  屏幕坐标
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = event.getRawX();
        y = event.getRawY();

        postInvalidate();
        return true;
    }

    public GameView(Context context) {
        super(context);
        //获取屏幕尺寸
        this.screenwidth = this.getResources().getDisplayMetrics().widthPixels;
        this.screenheight = this.getResources().getDisplayMetrics().heightPixels;
        //直线起始位置
        this.lineX = 0.0f;
        this.lineY = 0.0f;
        //矩形起始点
        this.x = screenwidth / 2 - 50;
        this.y = screenheight / 2 - 50;
        //圆半径
        this.radius = 10;
        this.paint = new Paint();
        paint.setAntiAlias(true);

        new DrawThread().start();
    }

    private class DrawThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (flag) {
                long _starttime = System.currentTimeMillis();
                //游戏逻辑
                logic();
                long _endtime = System.currentTimeMillis();

                //定义每帧
                long _sleeptime = speed * 40 - (_endtime - _starttime);

                if (_sleeptime > 0) {
                    try {
                        Thread.sleep(_sleeptime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                postInvalidate();
            }

        }
    }

    private void logic() {
        if (radius > 200) {
            Cdir = -1;
        }
        if (radius < 0) {
            Cdir = 1;
        }
        radius += Cdir * 5;
float step=10;
        switch(walk)
        {
            case 0://上
                lineY-=step;
                break;
            case 1://右
                lineX+=step;
                break;
            case 2://下
                lineY+=step;
                break;
            case 3://左
                lineX-=step;
                break;
        }
        if(lineX<0&&walk==3)walk=2;//下一个方向：下
        if(lineX>screenwidth&&walk==1)walk=3;//下一个方向：左
        if(lineY>screenheight&&walk==2)walk=0;//下一个方向：上
        if(lineY<0&&walk==0)walk=1;//下一个方向：右

    }
}
